//
//  HSMainAskVC.m
//  HolyShit
//
//  Created by Robin on 2016/9/27.
//  Copyright © 2016年 HolyShit. All rights reserved.
//

#import "HSMainAskVC.h"
#import "HSServiceCell.h"
#import "HSWebVC.h"
#import "HSMainAskVM.h"
#import "HSAskDetailVC.h"

@interface HSMainAskVC ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) HSMainAskVM *askVM;

@end

@implementation HSMainAskVC

- (instancetype)init
{
    if (self = [super init]) {
        self.title = @"行业资讯";
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.leftBarButtonItem = nil;
    [self.view addSubview:self.tableView];
    @weakify(self);
    [self.hintView setReloadBlock:^{
        @strongify(self);
        self.hintView.hintState = MYHintStateLoading;
        [self refresh];
    }];
    self.hintView.reloadBlock();
}

#pragma mark - method

- (void)refresh
{
    @weakify(self);
    [[self.askVM.refreshCmd execute:nil] subscribeNext:^(id x) {
        self.hintView.hintState = MYHintStateHidden;
        [self.tableView endRefreshAndLoadMore];
        [self.tableView reloadData];
    } error:^(NSError *error) {
        @strongify(self);
        self.hintView.hintState = MYHintStateNoData;
        [self.tableView endRefreshAndLoadMore];
    }];
}

- (void)loadmore
{
    @weakify(self);
    [[self.askVM.loadmoreCmd execute:nil] subscribeNext:^(id x) {
        [self.tableView endRefreshAndLoadMore];
        [self.tableView reloadData];
    } error:^(NSError *error) {
        @strongify(self);
        [self.tableView endRefreshAndLoadMore];
    }];
}

#pragma mark - delegate & datasouce

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.askVM.datas.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (ServiceType == 1) {
        return 330;
    }
    else if(ServiceType == 2){
        return 450;
    }
    return 290;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    HSServiceCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([HSServiceCell class])];
    cell.model = self.askVM.datas[indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    [self my_push:[[HSAskDetailVC alloc] initWithExpertId:self.askVM.datas[indexPath.row].expertId]];
}

#pragma mark - uitableview

- (UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.width, self.view.height - NavBarHeight - TabbarHeight)];
        _tableView.backgroundColor = WHITE_COLOR;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_tableView addRefreshTarget:self action:@selector(refresh)];
        [_tableView addLoadMoreTarget:self action:@selector(loadmore)];
        [_tableView registerClass:[HSServiceCell class] forCellReuseIdentifier:NSStringFromClass([HSServiceCell class])];
    }
    return _tableView;
}

- (HSMainAskVM *)askVM{
    if(!_askVM){
        _askVM = [HSMainAskVM new];
    }
    return _askVM;
}

@end
